﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Data.Mappers;

namespace TestsMyShop.Mappers_Test
{
    [TestFixture]
    public class Categories
    {
        [Test]
        public void Should_convert_category_from_entity_to_model()
        {
            // arange
            List<Entity.Products.Product> products = new List<Entity.Products.Product>();
            products.Add(new Entity.Products.Product() { Price = 10, Name = "Apple" });

            Entity.Categories.Category category = new Entity.Categories.Category() { Name = "fruit", Description = "fruit", Products = products };

            // act
            IMapper<Entity.Categories.Category, Data.ModelDatabase.Category> mapper = new CategoryMapper();
            Data.ModelDatabase.Category modelCategory = mapper.WrapperEntity(category);

            // assert
            Assert.AreEqual(category.Description, modelCategory.Description);
            Assert.AreEqual(category.Name, modelCategory.Name);

            for (int i = 0; i < products.Count; i++)
            {
                if (products[i].Id != modelCategory.Products.ToArray()[i].Id
                    && products[i].Name != modelCategory.Products.ToArray()[i].Name
                    && products[i].Price != modelCategory.Products.ToArray()[i].Price)
                        Assert.Fail();
            }
        }

        [Test]
        public void Should_convert_category_from_model_to_entity()
        {
            // arrange
            List<Data.ModelDatabase.Product> products = new List<Data.ModelDatabase.Product>();
            products.Add(new Data.ModelDatabase.Product() { Name = "Apple", Description = "Apple" });

            Data.ModelDatabase.Category modelCategory = new Data.ModelDatabase.Category() { Name = "fruit", Description = "fruit", Id = 1, Products = products };

            // act
            IMapper<Entity.Categories.Category, Data.ModelDatabase.Category> mapper = new CategoryMapper();
            Entity.Categories.Category category = mapper.WrapperData(modelCategory);

            // assert
            Assert.AreEqual(category.Description, modelCategory.Description);
            Assert.AreEqual(category.Name, modelCategory.Name);
            Assert.AreEqual(category.Id, modelCategory.Id);

            for (int i = 0; i < products.Count; i++)
            {
                if (products[i].Id != category.Products.ToList()[i].Id
                    && products[i].Name != category.Products.ToList()[i].Name
                    && products[i].Price != category.Products.ToList()[i].Price)

                    Assert.Fail();
            }
        }

        [Test]
        public void Should_convert_categories_list_from_entity_to_model()
        {
            // arange
            List<Entity.Products.Product> products = new List<Entity.Products.Product>();
            products.Add(new Entity.Products.Product() { Price = 10, Name = "Apple" });

            List<Entity.Categories.Category> categories = new List<Entity.Categories.Category>();
            categories.Add(new Entity.Categories.Category() { Name = "fruit", Description = "fruit", Products = products });
            categories.Add(new Entity.Categories.Category() { Name = "vegetables", Description = "vegetables", Products = products });

            // act
            IMapper<Entity.Categories.Category, Data.ModelDatabase.Category> mapper = new CategoryMapper();
            List<Data.ModelDatabase.Category> modelCategories = mapper.WrapperEntity(categories);

            // assert
            foreach (var cat in modelCategories)
            {
                Assert.AreEqual(cat.Description, cat.Description);
                Assert.AreEqual(cat.Name, cat.Name);

                for (int i = 0; i < products.Count; i++)
                {
                    if (products[i].Id != cat.Products.ToArray()[i].Id
                        && products[i].Name != cat.Products.ToArray()[i].Name
                        && products[i].Price != cat.Products.ToArray()[i].Price)

                        Assert.Fail();
                }
            }
        }
    }
}
